import React from 'react';
import { string, object } from 'prop-types'

import { Route, Redirect } from 'react-router-dom'
import { useSelector } from 'react-redux'

const PrivateRoute = ({ path, component }) => {
  const token = useSelector(state => state.user.token)
  if (token) {
    return <Route path={path} component={component} />
  }

  return (
    <Redirect to={'/login'} />
  )
}

PrivateRoute.prototype = {
  path: string.isRequired,
  component: object.isRequired,
}

export default PrivateRoute;
